
// Open MoneySpinner Suite v1
// An open source business management software system written in Java and MySQL
// Recommended IDE is NetBeans IDE 7.0.1
// Support Web Site: http://www.milliscript.com
//
// Copyright (C) 2014, Abiodun Aremu, Ibadan/NIGERIA.
// Open MoneySpinner Suite is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package openmoneyspinnersuite;

/**
 *
 * @author Abiodun Aremu
 */
import javax.swing.*;
import java.sql.*;

final class NewProfileMember extends javax.swing.JInternalFrame {

    Object[][] tableObject=new Object[][]{};
    Systems systems=new Systems();
    Thread processThread=new Thread();
    Thread titleThread=new Thread();
    PreparedStatement statement;
    String profileName="",addInUse,delInUse;
    String multiRow="",comment="";
    String[] tableColumn=new String[]{},defaultValue=new String[]{},typeValue=new String[]{},refTableValue=new String[]{},optionalValue=new String[]{},editableValue=new String[]{};
    boolean editableCell[]=new boolean[]{};
    /** Creates new form ViewCredit */
    public NewProfileMember() {
        initComponents();
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "Attribute"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        /* Execute thread */
        processThread=new Thread(new Runnable() {
            public void run() {
            disableControls();
            resetTable();
            resetProfileCode();
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("viewItemThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
        reset();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jRefreshButton = new javax.swing.JButton();
        jCancelButton = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jRegisterButton = new javax.swing.JButton();
        delButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jPrimaryMemberNameTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jCommentTextArea = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(openmoneyspinnersuite.OpenMS_Finance_App.class).getContext().getResourceMap(NewProfileMember.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setToolTipText(resourceMap.getString("Form.toolTipText")); // NOI18N
        setFrameIcon(resourceMap.getIcon("Form.frameIcon")); // NOI18N
        setName("Form"); // NOI18N
        setVisible(true);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Attribute", "Value"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(10);
        jTable1.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable1.columnModel.title0")); // NOI18N
        jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title4")); // NOI18N

        jRefreshButton.setFont(resourceMap.getFont("jRefreshButton.font")); // NOI18N
        jRefreshButton.setIcon(resourceMap.getIcon("jRefreshButton.icon")); // NOI18N
        jRefreshButton.setText(resourceMap.getString("jRefreshButton.text")); // NOI18N
        jRefreshButton.setName("jRefreshButton"); // NOI18N
        jRefreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRefreshButtonActionPerformed(evt);
            }
        });

        jCancelButton.setFont(resourceMap.getFont("jCancelButton.font")); // NOI18N
        jCancelButton.setIcon(resourceMap.getIcon("jCancelButton.icon")); // NOI18N
        jCancelButton.setText(resourceMap.getString("jCancelButton.text")); // NOI18N
        jCancelButton.setName("jCancelButton"); // NOI18N
        jCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelButtonActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Select --", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jRegisterButton.setIcon(resourceMap.getIcon("jRegisterButton.icon")); // NOI18N
        jRegisterButton.setText(resourceMap.getString("jRegisterButton.text")); // NOI18N
        jRegisterButton.setName("jRegisterButton"); // NOI18N
        jRegisterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRegisterButtonActionPerformed(evt);
            }
        });

        delButton.setIcon(resourceMap.getIcon("delButton.icon")); // NOI18N
        delButton.setText(resourceMap.getString("delButton.text")); // NOI18N
        delButton.setName("delButton"); // NOI18N
        delButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delButtonActionPerformed(evt);
            }
        });

        addButton.setIcon(resourceMap.getIcon("addButton.icon")); // NOI18N
        addButton.setText(resourceMap.getString("addButton.text")); // NOI18N
        addButton.setName("addButton"); // NOI18N
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Select --", "Item 2", "Item 3", "Item 4" }));
        jComboBox2.setEnabled(false);
        jComboBox2.setName("jComboBox2"); // NOI18N
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jPrimaryMemberNameTextField.setText(resourceMap.getString("jPrimaryMemberNameTextField.text")); // NOI18N
        jPrimaryMemberNameTextField.setEnabled(false);
        jPrimaryMemberNameTextField.setName("jPrimaryMemberNameTextField"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jCommentTextArea.setColumns(20);
        jCommentTextArea.setRows(5);
        jCommentTextArea.setName("jCommentTextArea"); // NOI18N
        jScrollPane2.setViewportView(jCommentTextArea);

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPrimaryMemberNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 921, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(140, 140, 140)
                        .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(delButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRegisterButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRefreshButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCancelButton)))
                .addContainerGap(264, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPrimaryMemberNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(88, 88, 88))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRefreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRegisterButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(delButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void jRefreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRefreshButtonActionPerformed
        
        /* Execute thread */
        processThread=new Thread(new Runnable() {
            public void run() {
            disableControls();
            refreshTable();
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("viewItemThread stopped runing.");
                    String vwProfile[]=systems.getValue(OpenMSApp.Database_A, "Select cMultiRow,tComment from vwProfile where vName='"+jComboBox1.getSelectedItem().toString()+"'", "cMultiRow","tComment");
                    multiRow=vwProfile[0];comment=vwProfile[1];System.out.println("Comment: "+comment);
                    System.out.println("Multirow attribute='"+multiRow+"'");
                    if(multiRow.trim().equalsIgnoreCase("0"))
                    {
                        addButton.setEnabled(false);
                        delButton.setEnabled(false);
                    }
                    else{
                        addButton.setEnabled(true);
                        delButton.setEnabled(true);
                    }
            }
        });
        processThread.start();
        titleThread.start();
    }//GEN-LAST:event_jRefreshButtonActionPerformed

    private void jCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelButtonActionPerformed
        
        if(processThread.isAlive())
        {
            try{
                processThread.stop();
                titleThread.stop();
                System.out.println("Threads successfully stopped.");
            }catch(Exception e){System.out.println("Thread stopping error: "+e);}
            this.setTitle("::. New Profile Member");
            enableControls();
        }
        else
        dispose();
    }//GEN-LAST:event_jCancelButtonActionPerformed
    void reset()
    {
        jPrimaryMemberNameTextField.setText("");
        jCommentTextArea.setText("");
        resetTable();
        resetProfileCode();
        resetPrimaryMemberCode();
    }
    void resetCombo()
    {
        resetTable();
        resetProfileCode();
        resetPrimaryMemberCode();
    }
    void addRow()
    {
        if(multiRow.trim().equalsIgnoreCase("0"))
        {
            return;
        }
        addInUse="Y";
        System.out.println("Add in use:"+addInUse);
        int x=tableObject.length;
        int y=x+1;
        Object[][] holdObject=new Object[y][tableColumn.length];

        for(int z=0;z<x;z++)
        {
            for(int u=0;u<optionalValue.length;u++)
            {
                if(optionalValue[u]!=null)
                {
                    if(jTable1.getValueAt(z,u+1)==null)jTable1.setValueAt("", z, u+1);
                    if(optionalValue[u].trim().equalsIgnoreCase("0")&&jTable1.getValueAt(z,u+1).toString().isEmpty())
                    {
                        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"NULL VALUE FOUND ! ","ERROR !",JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                }
            }
        }

        for(int z=0;z<x;z++)
        {
                holdObject[z][0]=z+1;
            for(int w=0;w<optionalValue.length;w++)
            {
                if(jTable1.getValueAt(z,w+1)==null)holdObject[z][w+1]="";else holdObject[z][w+1]=jTable1.getValueAt(z,w+1).toString();
            }
                holdObject[y-1][0]=y;
        }

        //System.out.println(holdObject[0][0]+""+holdObject[0][1]+""+holdObject[0][2]+""+holdObject[0][3]+""+holdObject[0][4]+""+holdObject[0][5]);

        tableObject=holdObject;
        int xu=1;
        for(String s:defaultValue)
        {
            tableObject[tableObject.length-1][xu++]=s;
        }
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,tableColumn
        ) {
            boolean[] canEdit = editableCell;

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        
        jTable1.getColumnModel().getColumn(0).setMaxWidth(40);        
        
        resetTableCombo();
        addInUse="N";
    }
    void deleteRow()
    {
        if(multiRow.trim().equalsIgnoreCase("0"))
        {
            return;
        }

        delInUse="Y";
        System.out.println("delete In Use"+delInUse);
        int x=tableObject.length;
        int y=x-1;
        Object[][] holdObject=new Object[y][tableColumn.length];

        if(x==1)
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Can Not Delete Row \n   Invalid Operation ! ","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }

        for(int z=0;z<x-1;z++)
        {
                holdObject[z][0]=z+1;
            for(int w=0;w<optionalValue.length;w++)
            {
                if(jTable1.getValueAt(z,w+1)==null)holdObject[z][w+1]="";else holdObject[z][w+1]=jTable1.getValueAt(z,w+1).toString();
            }
                holdObject[y-1][0]=y;
        }
        
        for(int z=0;z<x-1;z++)
        {
                holdObject[z][0]=z+1;
                
            for(int w=0;w<optionalValue.length;w++)
            {
                if(jTable1.getValueAt(z,w+1)==null)holdObject[z][w+1]="";else holdObject[z][w+1]=jTable1.getValueAt(z,w+1).toString();
            }
                
                holdObject[y-1][0]=y;
        }
        tableObject=holdObject;

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,tableColumn
        ) {
            boolean[] canEdit = editableCell;

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });

        jTable1.getColumnModel().getColumn(0).setMaxWidth(40);
        
        resetTableCombo();
        delInUse="N";
    }
    void resetProfileCode()
    {
        String[] orderCode=systems.getTableDataArray(OpenMSApp.Database_A,"vwProfile",2);
        orderCode[0]="-- Select --";
        jComboBox1.setModel(new DefaultComboBoxModel(orderCode));
    }
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        String profileCode=jComboBox1.getSelectedItem().toString();
        if(profileCode.equalsIgnoreCase("-- Select --"))
        {
            reset();
        }
        else
        {
            /* Execute thread */
            processThread=new Thread(new Runnable() {
                public void run() {
                    disableControls();
                    resetPrimaryMemberCode();
                    refreshTable();
                }
            });
            titleThread=new Thread(new Runnable() {
                public void run() {
                    while(processThread.isAlive())setProcessingTitle(titleThread);
                    enableControls();
                    System.out.println("newProfileMember stopped runing.");
                    String vwProfile[]=systems.getValue(OpenMSApp.Database_A, "Select cMultiRow,tComment from vwProfile where vName='"+jComboBox1.getSelectedItem().toString()+"'", "cMultiRow","tComment");
                    multiRow=vwProfile[0];comment=vwProfile[1];
                    System.out.println("comment='"+comment+"'");
                    System.out.println("Multirow attribute='"+multiRow+"'");
                        if(multiRow.trim().equalsIgnoreCase("0"))
                        {
                            addButton.setEnabled(false);
                            delButton.setEnabled(false);
                            jComboBox2.setEnabled(false);
                        }
                        else{
                            resetPrimaryMemberCode();
                            addButton.setEnabled(true);
                            delButton.setEnabled(true);
                            jComboBox2.setEnabled(true);
                        }
                }
            });
            processThread.start();
            titleThread.start();

        }
}//GEN-LAST:event_jComboBox1ActionPerformed
void resetPrimaryMemberCode()
{
    String refTableCode=systems.getValue(OpenMSApp.Database_A, "Select * from vwProfile where vName='"+jComboBox1.getSelectedItem().toString()+"'", "cPrimaryTableCode");
    String primaryTable=systems.getValue(OpenMSApp.Database_A, "Select * from referenceTable where cReferenceTableCode='"+refTableCode+"'", "vName");
    System.out.println("primary table name="+primaryTable);
    String[] orderCode=systems.getTableDataArray(OpenMSApp.Database_A," vwMerchant where vTable='"+primaryTable.replace(' ', '_') +"'",1);
    orderCode[0]="-- Select --";
    jComboBox2.setModel(new DefaultComboBoxModel(orderCode));
}
void resetTableCombo()
    {
        for(int x=0;x<typeValue.length;x++)
        {
            if(typeValue[x].equalsIgnoreCase("reference"))
            {
                String[] dbItem=systems.getTableDataArray(OpenMSApp.Database_A, " vwMerchant where vTable='"+refTableValue[x].trim().replace(' ', '_') +"'", 1);
                javax.swing.JComboBox combo=new javax.swing.JComboBox(dbItem);
                javax.swing.table.TableColumn comboColumn=jTable1.getColumnModel().getColumn(x+1);
                comboColumn.setCellEditor(new javax.swing.DefaultCellEditor(combo));            
            }
        }
    }
    void resetTable()
    {
        tableObject=new Object[][]{};
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "Attribute"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
    }
private void jRegisterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRegisterButtonActionPerformed
        if(jComboBox1.getSelectedItem().toString().equalsIgnoreCase("-- Select --"))
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE SELECT A PROFILE NAME !","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }
        for(int z=0;z<tableObject.length;z++)
        {
            String value="";
            for(int u=0;u<optionalValue.length;u++)
            {
                if(optionalValue[u]!=null)
                {
                    if(optionalValue[u].equalsIgnoreCase("0")&&jTable1.getValueAt(z,u+1).toString().isEmpty())
                    {
                        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"NULL VALUE FOUND ! ","ERROR !",JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                }
                if(jTable1.getValueAt(z,u+1)!=null)value=jTable1.getValueAt(z,u+1).toString();
                
                if(typeValue[u].equalsIgnoreCase("Decimal")&&!systems.isStringDecimalNeg(value.trim()))
                {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"ERROR AT ROW:"+(z+1)+"\nPLEASE ENTER DECIMAL VALUE FOR ATTRIBUTE '"+tableColumn[u+1].toUpperCase()+"'","ERROR !",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if(typeValue[u].equalsIgnoreCase("Integer")&&!systems.isStringDigit(value.trim()))
                {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"ERROR AT ROW:"+(z+1)+"\nPLEASE ENTER INTEGER VALUE FOR ATTRIBUTE '"+tableColumn[u+1].toUpperCase()+"'","ERROR !",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if(typeValue[u].equalsIgnoreCase("Date/time")&&!systems.isSQLDateFormat(value.trim()))
                {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"ERROR AT ROW:"+(z+1)+"\nPLEASE ENTER DATE VALUE (YYYY-MM-DD) FOR ATTRIBUTE '"+tableColumn[u+1].toUpperCase()+"'","ERROR !",JOptionPane.ERROR_MESSAGE);
                    return;
                }                
            }
        }            
        registerProfileMember();
}//GEN-LAST:event_jRegisterButtonActionPerformed
    void registerProfileMember()
    {
        profileName=jComboBox1.getSelectedItem().toString().trim().replace(' ', '_');
        String profileTableCode=systems.getValue(OpenMSApp.Database_A, "Select cReferenceTableCode from referenceTable where vName='"+jComboBox1.getSelectedItem().toString()
                +"'", 1);
        String profileMemberCode=systems.getValue(OpenMSApp.Database_A, Connect.procInit+" prnPrint"+profileName+"Code"+Connect.procInitStart+Connect.procInitEnd, "cCode");
        if(multiRow.equalsIgnoreCase("1"))
        {
            if(jComboBox2.getSelectedItem().toString().trim().equalsIgnoreCase("-- Select --"))
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Please select a primary member code ","ERROR !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            profileMemberCode=jComboBox2.getSelectedItem().toString().trim();
        }
        String serialArray[]=systems.getColumn(OpenMSApp.Database_A, "Select * from "+profileName+" where c"+profileName+"code='"+profileMemberCode+"'", tableColumn.length+3);
        int serialNumber=0;
        if(serialArray.length>0)
        serialNumber=Integer.parseInt(serialArray[serialArray.length-1]);
        System.out.println("profilemebercode"+profileMemberCode);
        System.out.println("MilliscriptDMSApp.LoginCode"+OpenMSApp.LoginCode);
        System.out.println("serialNumber"+serialNumber);
        System.out.println("jCommentTextArea.getText().trim()"+jCommentTextArea.getText().trim());
        String comm=jCommentTextArea.getText().trim();
        if(comm==null)comm="";
        Connect.changeDB(OpenMSApp.Database_A);
        try
        { 
            for(int x=0;x<tableObject.length;x++)
            {                
                statement=Connect.createStatement(" INSERT INTO "+profileName+" VALUES('"+profileMemberCode.trim()+"','"+OpenMSApp.LoginCode+"',NOW(),"+systems.generatePeriod(tableColumn.length-1)+","+(++serialNumber)+" )");
        
                int yu=tableColumn.length-1,y=0;
                while(y<tableColumn.length-1)
                //for(int y=0;y<tableColumn.length-1;y++)
                {
                    String vlu="";
                    if(jTable1.getValueAt(x,y+1)!=null)vlu=jTable1.getValueAt(x,y+1).toString();
                    statement.setString((y+1),vlu);
                    y++;
                }
                Connect.executeUpdateStatement(statement);
            }
            if(comment.equalsIgnoreCase("1"))
            {
                System.out.println("right here!");
                statement=Connect.createStatement(Connect.procInit+" prnInsProfileMemberComment"+Connect.procInitStart+"?,?,?"+Connect.procInitEnd);

                statement.setString(1,profileMemberCode);
                statement.setString(2,profileTableCode.trim());   
                statement.setString(3,comm);

                Connect.executeUpdateStatement(statement);
            }
        }
        catch(Exception e)
        {
            System.out.println("NewProfileMember.registerProfileMember() gave error ! "+e);
        }
        Connect.alert="Y";
        Connect.closeConnection();
        //TO AVOID RESET DESPITE OF THE ABOVE RETURN STATEMENT IN ACTION PERFORMED, RESET() IS BROUGHT BELOW
        reset();
    }
private void delButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delButtonActionPerformed
    deleteRow();
}//GEN-LAST:event_delButtonActionPerformed

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
    addRow();
}//GEN-LAST:event_addButtonActionPerformed

private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
    String primaryMemberCode=jComboBox2.getSelectedItem().toString().trim();
    if(primaryMemberCode.equalsIgnoreCase("-- Select --"))
        resetPrimaryMemberCode();
    else
        jPrimaryMemberNameTextField.setText(systems.getValue(OpenMSApp.Database_A,"Select * from vwMerchant where cMerchantID='"+primaryMemberCode+"'",2));
}//GEN-LAST:event_jComboBox2ActionPerformed
    private void disableControls()
    {
        jRefreshButton.setEnabled(false);
        jRegisterButton.setEnabled(false);
        addButton.setEnabled(false);
        delButton.setEnabled(false);
    }                                             
    private void enableControls()
    {
        jRefreshButton.setEnabled(true);
        jRegisterButton.setEnabled(true);
        addButton.setEnabled(true);
        delButton.setEnabled(true);        
        this.setTitle("::. New Profile Member");
    }                                                   
    void print()
    {
        try
        {
            jTable1.print();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
                       
    private void setProcessingTitle(Thread thread)
    {
        try{
            this.setTitle("::. New Profile Member - Processing Request");
            thread.sleep(500);
            this.setTitle("::. New Profile Member - Processing Request.");
            thread.sleep(500);
            this.setTitle("::. New Profile Member - Processing Request..");
            thread.sleep(500);
            this.setTitle("::. New Profile Member - Processing Request...");
            thread.sleep(500);
        }catch(Exception e){System.out.println("Title thread"+e);}
    }
    void refreshTable()
    {
        String profileCode=systems.getValue(OpenMSApp.Database_A, "Select * from referenceTable where vName='"+jComboBox1.getSelectedItem().toString().trim()+"'", 1);
        String[][] profileValues=systems.getColumn(OpenMSApp.Database_A, "Select * from profile where cProfileCode='"+profileCode+"' order by cProfileCode,iSerial", 2,3,4,6,7,8);
        tableColumn=systems.toColumnHeaderWithLead(systems.stringArrayToTitleCase(profileValues[0]));
        typeValue=profileValues[1];
        defaultValue=profileValues[2];
        refTableValue=profileValues[3];
        optionalValue=profileValues[4];
        editableValue=profileValues[5];
        //initialize tableObject with row one
        tableObject=new Object[1][defaultValue.length+1];
        tableObject[0][0]="1";
        //initialize tableObject with defaultValue
        int x=1,y=1,maxRowLen=0;
        for(String s:defaultValue)
        {
            if(s==null)s="";
            tableObject[0][x++]=s;
        }
        x=1;
        for(String columnName:tableColumn)
        {
            String columnConstant[]=systems.getColumn(OpenMSApp.Database_A, "Select vConstant from profileConstant where cProfileCode='"+systems.getValue(OpenMSApp.Database_A, "Select cReferenceTableCode from referenceTable where vName='"+jComboBox1.getSelectedItem().toString().toString()+"'",1)+"' and vAttribute='"+columnName+"'", 1);

            if(y<columnConstant.length)
            y=columnConstant.length;
        }
        tableObject=new Object[y][tableColumn.length];
        for(int u=0;u<y;u++)tableObject[u][0]=u+1;
        x=0;maxRowLen=y;y=0;
        for(String columnName:tableColumn)
        {
            String columnConstant[]=systems.getColumn(OpenMSApp.Database_A, "Select vConstant from profileConstant where cProfileCode='"+systems.getValue(OpenMSApp.Database_A, "Select cReferenceTableCode from referenceTable where vName='"+jComboBox1.getSelectedItem().toString().toString()+"'",1)+"' and vAttribute='"+columnName+"'", 1);
            for(;y<columnConstant.length&&y<maxRowLen;y++)
                if(columnConstant[y]==null)tableObject[y][x]="";else tableObject[y][x]=columnConstant[y];            
            x++;
        }
        
        //set cells editable
        editableCell=systems.generateBooleanArrayTrue(tableColumn.length);
        //set column 1 not editable
        editableCell[0]=false;
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,tableColumn
        ) {
            boolean[] canEdit = editableCell;
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });

        jTable1.getColumnModel().getColumn(0).setMaxWidth(40);
        addButton.setEnabled(true);
        delButton.setEnabled(true);
        resetTableCombo();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton delButton;
    private javax.swing.JButton jCancelButton;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JTextArea jCommentTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jPrimaryMemberNameTextField;
    private javax.swing.JButton jRefreshButton;
    private javax.swing.JButton jRegisterButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
